package top.yqingyu.trans$client.main;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import top.yqingyu.qymsg.MsgHelper;
import top.yqingyu.qymsg.QyMsg;

/**
 * @author YYJ
 * @version 1.0.0
 * @date 2022/4/23 19:31
 * @description
 * @modified by
 */
public class PrintMsgThread implements Runnable {
    private final TransClient client;
    private final static Logger logger = LoggerFactory.getLogger(PrintMsgThread.class);

    private PrintMsgThread(TransClient client) {
        this.client = client;
    }

    public static void init(TransClient client) {
        Thread th = new Thread(new PrintMsgThread(client));
        th.setDaemon(true);
        th.setName("PrintMsg");
        th.start();
    }

    @Override
    public void run() {
        while (client.running.get()) {
            try {
                QyMsg msg = client.RSP_MSG_QUEUE.take();
                System.out.print(MsgHelper.gainMsg(msg));
            } catch (Exception e) {
                logger.error("", e);
                client.running.set(false);
            }
        }
    }
}
